﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Text.RegularExpressions;

namespace auxiliary_proj
{
    public partial class FormUnitSummary : Form
    {
        int unit_id = 0;
        private static MySqlConnection DBCon;

        //Default Constructor
        public FormUnitSummary()
        {
            InitializeComponent();
            DBCon = new MySqlConnection(Program.getConnection());
        }
        //Parameterized Constructor
        public FormUnitSummary(string unitName, int unit_id)
        {
            InitializeComponent();
            this.lbUnitName.Text = unitName;
            this.unit_id = unit_id;
            DBCon = new MySqlConnection(Program.getConnection());

            ///////////////////////////////////////////////////////////////
            ////////////////////  ALL_ITEMID_DESC  ////////////////////////
            ///////////////////////////////////////////////////////////////
            Query.ALL_ITEMID_DESC.Clear();
            Query.populateHashtable("ALL_ITEMID_DESC", "");


            ///////////////////////////////////////////////////////////////
            ////////////////////  UNIT_ITEMID_DESC  ///////////////////////
            ///////////////////////////////////////////////////////////////
            Query.UNIT_ITEMID_DESC.Clear();
            Query.populateHashtable("UNIT_ITEMID_DESC", "WHERE unit_id='" + unit_id + "' ");

            populateUNIT_ITEM();

            populateALL_ITEM();
        }

        public void populateUNIT_ITEM()
        {
            int[] unit_item_ids = new int[Query.UNIT_ITEMID_DESC.Count];
            string[] unit_item_desc = new string[Query.UNIT_ITEMID_DESC.Count];

            Query.UNIT_ITEMID_DESC.Keys.CopyTo(unit_item_ids, 0);
            Query.UNIT_ITEMID_DESC.Values.CopyTo(unit_item_desc, 0);

            for (int i = 0; i < unit_item_desc.Length; i++)
            {
                unit_item_desc[i] = unit_item_desc[i] + " - " + unit_item_ids[i];
            }

            //add array of values to the combo-box
            lbUnitItems.Items.Clear();
            lbUnitItems.Items.AddRange(unit_item_desc);
            ///////////////////////////////////////////////////////////////
            //remove key/values from all_itemid_desc that unit_itemid_desc has
            ///////////////////////////////////////////////////////////////
            foreach (int id in unit_item_ids)
            {
                if (Query.ALL_ITEMID_DESC.ContainsKey(id))
                    Query.ALL_ITEMID_DESC.Remove(id);
            }
        }

        public void populateALL_ITEM()
        {
            //after removing items from all_itemid_desc display
            int[] all_item_ids = new int[Query.ALL_ITEMID_DESC.Count];
            string[] all_item_desc = new string[Query.ALL_ITEMID_DESC.Count];

            Query.ALL_ITEMID_DESC.Keys.CopyTo(all_item_ids, 0);
            Query.ALL_ITEMID_DESC.Values.CopyTo(all_item_desc, 0);

            for (int i = 0; i < all_item_desc.Length; i++)
            {
                all_item_desc[i] = all_item_desc[i] + " - " + all_item_ids[i];
            }

            //add array of values to the combo-box
            lbAllItems.Items.Clear();
            lbAllItems.Items.AddRange(all_item_desc);
        }
        //delete items selected from the listbox for a unit

        //Adds all the items selected to a particular unit's inventory
        private void btnAddItems_Click(object sender, EventArgs e)
        {
            //remove key/value from all_itemid_desc, insert same key/value into unit_itemid_desc
            for (int i = 0; i < lbAllItems.SelectedItems.Count; i++)
            {
                String str = lbAllItems.SelectedItems[i].ToString();

                //add items to unit listbox
                lbUnitItems.Items.Add(str);

                //convert selected key/value into formated string and add to unit list (it auto sorts)
                String[] split = str.Split('-');
                //extract key/value from all item
                String value = split[0].Trim();
                int key = int.Parse(split[1].Trim());

                //remove from all, add to unit
                Query.ALL_ITEMID_DESC.Remove(key);
                Query.UNIT_ITEMID_DESC.Add(key, value);

                //insert into db
                string insert = "INSERT INTO ITEM_UNIT (unit_id, item_id)" +
                                    " VALUES ('" + this.unit_id + "', '" + key + "')";

                Query.nonQuery(insert);
            }

            populateALL_ITEM();
        }
        //Deletes all the items selected
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //remove key/value from all_itemid_desc, insert same key/value into unit_itemid_desc
            for (int i = 0; i < lbUnitItems.SelectedItems.Count; i++)
            {
                String str = lbUnitItems.SelectedItems[i].ToString();

                //remove from unit listbox, add to all item listbox
                lbAllItems.Items.Add(str);

                //convert selected key/value into formated string and add to unit list (it auto sorts)
                String[] split = str.Split('-');
                //extract key/value from all item
                String value = split[0].Trim();
                int key = int.Parse(split[1].Trim());

                //remove from unit, add to all
                Query.UNIT_ITEMID_DESC.Remove(key);
                Query.ALL_ITEMID_DESC.Add(key, value);

                //delete from db
                string delete = "DELETE FROM ITEM_UNIT WHERE item_id='" + key + "' AND unit_id='" + this.unit_id + "'";
                Query.nonQuery(delete);
            }
            populateUNIT_ITEM();
        }
    }
}
